分段故障 - 矩阵的转置 - C(Segmentation Fault - Transpose of a Matrix - C)
我在以下代码上有分段错误错误:
struct matrix { int nl, nc ; int** mat ; }; Matrix* initMatrix (int nlines, int ncol) { struct matrix* mat ; mat = (struct matrix*)malloc(sizeof(struct matrix)) ; mat->nl = nlines ; mat->nc = ncol ; int i ; mat->mat = (int **)malloc((mat->nl)*sizeof(int *)) ; for (i=0;i<(mat->nl); i++) { mat->mat[i] = (int*)malloc((mat->nc)*sizeof(int)) ; } return mat ; } Matrix* transp (Matrix* mat) { int i, j; int linesTrp = mat->nc ; int colTrp = mat->nl ; Matrix* trp = initMatrix (linesTrp, colTrp) ; for (i=0; i<(linesTrp); i++) { for (j=0; j<(colTrp); j++) { trp->mat[j][i] = mat->mat[i][j] ; } } return trp; }
显然,程序在到达此行时会给出分段错误消息:
for (j=0; j<(colTrp); j++) {
如果有人可以帮助我,我将不胜感激。 对不起最后的坏英语(我来自巴西)
I have a segmentation fault error on the following code:
struct matrix { int nl, nc ; int** mat ; }; Matrix* initMatrix (int nlines, int ncol) { struct matrix* mat ; mat = (struct matrix*)malloc(sizeof(struct matrix)) ; mat->nl = nlines ; mat->nc = ncol ; int i ; mat->mat = (int **)malloc((mat->nl)*sizeof(int *)) ; for (i=0;i<(mat->nl); i++) { mat->mat[i] = (int*)malloc((mat->nc)*sizeof(int)) ; } return mat ; } Matrix* transp (Matrix* mat) { int i, j; int linesTrp = mat->nc ; int colTrp = mat->nl ; Matrix* trp = initMatrix (linesTrp, colTrp) ; for (i=0; i<(linesTrp); i++) { for (j=0; j<(colTrp); j++) { trp->mat[j][i] = mat->mat[i][j] ; } } return trp; }
Apparently, the program gives me the segmentation fault message when it reaches this line:
for (j=0; j<(colTrp); j++) {
Please, if anyone can help me, I would appreciate. Also, sorry for eventual bad english (I'm from Brazil)
原文:https://stackoverflow.com/questions/19757081
最满意答案
WinSCP .NET程序集
Session
API不能使用流提供下载文件的内容。所以你要做的就是使用
Session.GetFiles
将远程文件下载到本地临时位置并从那里读取文件:// Generate unique file name for the temporary file string tempPath = Path.GetTempFileName(); // Download the remote file to the temporary location session.GetFiles("/path/file.ext", tempPath).Check(); try { // Open the temporarily downloaded file for reading using (Stream stream = File.OpenRead(tempPath)) { // use the stream blockBlob.UploadFromStream(fileStream); blobUri = blockBlob.Uri.ToString(); } } finally { // Discard the temporarily downloaded file File.Delete(tempPath); }
The WinSCP .NET assembly
Session
API cannot not provide the contents of downloaded file using streams.So all you can do, is to download the remote file to a local temporary location using the
Session.GetFiles
and read the file from there:// Generate unique file name for the temporary file string tempPath = Path.GetTempFileName(); // Download the remote file to the temporary location session.GetFiles("/path/file.ext", tempPath).Check(); try { // Open the temporarily downloaded file for reading using (Stream stream = File.OpenRead(tempPath)) { // use the stream blockBlob.UploadFromStream(fileStream); blobUri = blockBlob.Uri.ToString(); } } finally { // Discard the temporarily downloaded file File.Delete(tempPath); }
相关问答
更多-
无法使用WinSCP .NET程序集将文件下载到目录中(Unable to download file into directory using WinSCP .NET assembly)[2023-08-05]
这段代码应该有效: GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/"); This code should work: GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/"); -
看起来您假设该命令将文件解压缩到/
。 它不会。 它将它们提取到当前工作目录(可能是/home/user )。 使用-C (或--directory= )开关指定输出目录: session.ExecuteCommand("tar -xzf / /mytarball.tar.gz -C / "); It looks like you assume that the command will extract the files to / . It won't. It ... -
你不应该这样做。 将程序集与WinSCP应用程序配置耦合可能会给您带来很多麻烦。 默认情况下,程序集故意与WinSCP应用程序隔离。 使用SessionOptions类无法配置SessionOptions 。 使用其属性,用于基本选项; 或者使用SessionOptions.AddRawSettings方法进行高级设置 。 简单的方法,就是让WinSCP为您生成代码 。 过去,您可以滥用Session.DefaultConfiguration和SessionOptions.HostName来打开存储的会话。 ...
-
如果您可以在远程面板中看到所有1925文件,请使用命令>比较目录命令来查找WinSCP,缺少什么。 虽然我猜真正的问题是,所有文件都是实际下载的。 但是你在Linux目录中有相同名称的文件,只是一个不同的情况。 Windows不区分大小写,因此FILE.txt文件在下载时会覆盖file.txt 。 在这种情况下, Compare directories命令也不会发现任何差异。 If you can see all the 1925 files in the remote panel, use the Com ...
-
使用WinSCP .NET程序集将远程文件内容作为流访问(Access remote file contents as a stream using WinSCP .NET assembly)[2023-06-10]
WinSCP .NET程序集Session API不能使用流提供下载文件的内容。 所以你要做的就是使用Session.GetFiles将远程文件下载到本地临时位置并从那里读取文件: // Generate unique file name for the temporary file string tempPath = Path.GetTempFileName(); // Download the remote file to the temporary location session.GetFiles ... -
WinSCP .NET程序集 - 日志记录期间发生错误。(WinSCP .NET assembly - Error occurred during logging. It's been turned off)[2023-09-05]
“在记录期间发生错误,它已被关闭”的例外情况总是包含详细说明问题的更多信息。 最常见的原因是日志路径不存在,或者不能由运行该进程的帐户写入。 WinSCP.SessionRemoteException:日志记录期间发生错误。 它被关闭了。 ---> 无法打开日志文件'X:\ winscp.log'。 系统错误。 代码:2。 该系统找不到指定的文件 The exception "Error occurred during logging, it's been turned off" always includ ... -
使用Session.ListDirectory检索远程目录中的文件列表 迭代列表以查找符合条件的文件( .txt ?) 使用Session.GetFiles将匹配的文件下载到本地临时文件 阅读临时文件并查找所需内容 // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserNa ...
-
我想你的项目是64位,而WinSCP .NET程序集的分布式二进制文件只有32位。 将项目切换为32位或从源代码构建64位版本的程序集。 另请注意,您无需注册程序集( regasm )。 这仅适用于COM互操作。 直接使用它。 I suppose your project is 64-bit, while the distributed binary of WinSCP .NET assembly is only 32-bit. Either switch your project to 32-bit or ...
-
在他们的网站上我发现了这个: %WINDIR%\Microsoft.NET\Framework\
\RegAsm.exe WinSCPnet.dll /codebase /tlb %WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll/codebase /tlb:WinSCPnet32.tlb %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe Wi ... -
使用WinSCP .NET程序集列出具有特定扩展名的文件(List files with specific extension using WinSCP .NET assembly)[2022-04-09]
使用Session.ListDirectories方法 : RemoteDirectoryInfo directory = session.ListDirectory("/home/martin"); foreach (RemoteFileInfo fileInfo in directory.Files) { string extension = Path.GetExtension(fileInfo.Name); if (string.Compare(extension, ".txt", ...